﻿#region

using System;
using MediaSage.Lib.Utilities;
using NHibernate;
using NHibernate.Criterion;

#endregion

namespace MediaSage.Lib.Database
{
    public class MetadataEntity : DatabaseObject
    {
        #region Properties

        public virtual Guid ID { get; protected set; }

        public virtual string Key { get; set; }

        public virtual string Value { get; set; }

        #endregion Properties

        public override void Save()
        {
            ISession session = DBUtil.GetSession();
            using (session.BeginTransaction())
            {
                var dbCE =
                    session.CreateCriteria(typeof(MetadataEntity)).Add(Restrictions.Eq("Key", Key)).UniqueResult
                        <MetadataEntity>();
                if (dbCE != null)
                {
                    ID = dbCE.ID;
                    Key = dbCE.Key;
                    Value = dbCE.Value;
                }
            }
            base.Save();
        }

        public override void SaveOrUpdate()
        {
            ISession session = DBUtil.GetSession();
            using (session.BeginTransaction())
            {
                var dbCE =
                    session.CreateCriteria(typeof(MetadataEntity)).Add(Restrictions.Eq("Key", Key)).UniqueResult
                        <MetadataEntity>();
                if (dbCE != null)
                {
                    ID = dbCE.ID;
                    Key = dbCE.Key;
                    Value = dbCE.Value;
                }
            }
            base.SaveOrUpdate();
        }
    }
}